var host = 'http://daka.weiyingjia.org/api/base/';

//填写信息
function writeLuck(id,data,success,error){
  data.id = id;
  postMethod('write-luck', data,res=>{
    success();
  },err=>{
    error(err.message);
  })
}

//我的抽奖 1进行中 3未中奖 2已中奖
function myLuck(type,success,error){
  getMethod("my-luck",{type:type},res=>{
    success(res.list);
  },err=>{
    error(err.message);
  },true);
}

//参加幸运抽奖
function luckJoin(id,form_id,success,error){
  postMethod("luck-join",{id:id,form_id:'1'},res=>{
    success();
  },res=>{
    error(res.message);
  });
}

//接入完成
//幸运抽奖详情
function luckInfo(id,success,error){
  getMethod('luck-info',{id:id},res=>{
    success(res.info);
  },res=>{
    error(res.message)
  },true);
}

//接入完成
//幸运抽奖
function luck(type, success, error) {
  getMethod('luck', {
    type: type
  }, res => {
    success(res.list);
  }, res => {
    error(res.message);
  }, true);
}

//接入完成
//活动列表
function activityList(success) {
  getMethod('activity-list', {}, res => {
    success(res.list);
  }, null, true);
}
//接入完成
//兑换订单
function pointOrders(page, success) {
  getMethod('pointorders', {
    page: page
  }, res => {
    success(res.data, res.meta && res.meta.pagination ? res.meta.pagination.total_pages : 1);
  }, null, true);
}

//接入完成
//活动订单
function activeOrders(page, success) {
  getMethod('active-order', {
    page: page
  }, res => {
    success(res.order_list, res.meta && res.meta.pagination ? res.meta.pagination.total_pages : 1);
  }, null, true);
}
//接入完成
//兑换商品
function changeGoods(data, success, error) {
  postMethod('change-goods/' + data.id + "/" + data.number, data, res => {
    success();
  }, res => {
    error(res.message);
  });
}

//接入完成
//获取历史
function sendStepHistory(page, success) {
  getMethod('send-step-history', {
    page: page
  }, res => {
    success(res.data, res.meta && res.meta.pagination ? res.meta.pagination.total_pages : 1);
  }, null, true);
}
//接入完成
//获取互助列表
function helpRecordData(open_id, success) {
  getMethod('get-send-step-data', {
    open_id: open_id
  }, res => {
    success(res);
  }, null, true);
}
//接入完成
//赠送步数
function sendStep(uid, step, success, error) {
  postMethod('send-step', {
    send_user_id: uid,
    step: step
  }, res => {
    success();
  }, res => {
    error(res.message);
  })
}

//生成二维码图片
function qrCode() {
  postMethod('create-qr-code', {}, res => {
    success(res.data);
  });
}

//接入完成
//获取分享背景
function getShareBack(success) {
  wx.getSystemInfo({
    success: res => {
      var type = res.system.indexOf('iOS') != -1 ? 2 : 1;
      getMethod('get-share-back/' + type, {}, res => {
        success(res.data, res.sort, res.step);
      }, null, true);
    }
  });
}

//接入完成
//排行榜信息
function rankList(success) {
  getMethod('rank-list', {}, res => {
    success(res.data);
  }, null, true);
}
//接入完成
//点击气泡
function receiveBubble(point_type, success) {
  postMethod('receive-bubble', {
    point_type: point_type
  }, res => {
    success();
  });
}
//接入完成
//验证赠送用户手机号
function giveCheckPhone(phone, success, error) {
  postMethod('check-phone', {
    phone: phone
  }, res => {
    success(res.data);
  }, res => {
    error(res.message);
  });
}

//接入完成
//获取我的卡片
function myCard(success, error) {
  getMethod('my-card', {}, res => {
    success(res);
  }, res => {
    error(res.message);
  }, true);
}
//接入完成
//赠送卡牌
function sendCard(user_id, card, success, error) {
  postMethod('send-card', {
    user_id: user_id,
    card: JSON.stringify(card)
  }, res => {
    success();
  }, res => {
    error(res.message);
  });
}
//接入完成
//兑换卡牌
function exchangeCard(card, success, error) {
  postMethod('card-change', {
    change_count: card
  }, res => {
    success();
  }, res => {
    error(res.message);
  })
}

//接入完成
//获取规则说明 0  规则  1 签到规则
function getStepRule(type = 0, success) {
  getMethod('get-step-rule', {}, res => {
    success(type == 0 ? res.data : res.sign);
  }, null, true);
}

//接入完成
//补签
function repairSign(date, success, error) {
  postMethod("repair-sign", {
    date: date
  }, res => {
    success(res);
  }, err => {
    if (err != null) {
      error(err.message);
    }
  });
}

// 接入完成
// 签到
function sign(success, error) {
  postMethod("sign", {}, res => {
    success(res.data);
  }, error);
}

//接入完成
//获取签到数据
function signDatas(success, error) {
  postMethod("sign-data", {}, res => {
    success(res);
  }, error);
}

//接入完成
//获取分享设置
function shareSetting(success) {
  getMethod('get-share-setting', {}, res => {
    success(res);
  });
}

//促销商品
function sales(page, success) {
  getMethod('sales', {
    page: page
  }, res => {
    success(res.data, res.meta.pagination.total_pages);
  });
}

//接入完成
//获取商品
function goods(type, page, position, success) {
  getMethod('pointgoodslist', {
    page: page,
    type: type,
    position: position
  }, res => {
    success(res.data, res.meta.pagination.total_pages);
  });
}
//接入完成
//获取商品详情
function goodInfo(id, goods_type, success) {
  getMethod('pointgoods/' + id + '/' + 1, {}, res => {
    success(res);
  });
}

//接入完成
//获取token
function token(code, success) {
  postMethod('minicode', {
    code: code
  }, res => {
    success(res.data);
  }, null, false);
}
//接入完成
//更新用户信息
function updateUserInfo(uid, nickName, avatarUrl) {
  postMethod('update-user-info', {
    uid: uid,
    nickName: nickName,
    avatarUrl: avatarUrl
  }, res => {

  });
}
//接入成功
//用户是否首次登陆
function checkUserFirstLogin(success) {
  postMethod('check-user-first-login', {}, res => {
    success(res);
  });
}
//接入成功
//获取用户步数信息
function getUserStep(uid, session, iv, encryptData, success) {
  postMethod('get-user-step', {
    uid: uid,
    session: session,
    iv: encodeURIComponent(iv),
    encryptData: encodeURIComponent(encryptData),
  }, res => {
    success(res);
  }, null, true);
}
//接入成功
//获取用户手机号
function getUserPhone(uid, session, iv, encryptData, success) {
  // console.log(session+"\n"+iv+"\n"+encryptData);
  postMethod('get-user-phone', {
    uid: uid,
    session: session,
    iv: encodeURIComponent(iv),
    encryptData: encodeURIComponent(encryptData),
  }, res => {
    success(res.phone_number);
  });
}

//接入完成
// 获取banner列表 1首页 2二级
function banners(type, success) {
  getMethod('banners', {
    type: type
  }, res => {
    success(res.data);
  });
}
//接入完成
//banner 用户点击
function bannerLog(id) {
  postMethod('banner-log', {
    id: id
  }, res => {

  });
}
// get获取
function getMethod(url, data, success, error = null, token = false) {
  requestMethod("GET", url, data, success, error, token);
}
// post获取
function postMethod(url, data, success, error = null, token = true) {
  requestMethod("POST", url, data, success, error, token);
}

function requestMethod(method, url, data, success, error, token) {
  var postData = data;
  var header = {};

  var app = getApp();
  // var tokenTest = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vZGFrYS53ZWl5aW5namlhLm9yZy9hcGkvYmFzZS9taW5pY29kZSIsImlhdCI6MTU1NDg2OTE0MywiZXhwIjoxNTU0OTU1NTQzLCJuYmYiOjE1NTQ4NjkxNDMsImp0aSI6ImVNS09vaW13emp1MHUxdmciLCJzdWIiOjQyLCJwcnYiOiJhN2Q2NDdjYWRhYThmYTNlOGJkZTBiNWI4NmY1N2QxNGIwM2EwNWQ2In0.Jq4xeq5y-qHcpmYwoll3hQfVw7Qbg44248w6euw1VGI";
  // header = {
  //   Authorization: "Bearer " + tokenTest,
  // }

  if (token && app.globalData != null && app.globalData.userStatus.token != null) {
    header['Authorization'] = "Bearer " + app.globalData.userStatus.token;
  }

  if (method == "POST") {
    header['Content-Type'] = 'application/x-www-form-urlencoded'
  } else {
    header['Content-Type'] = 'application/json'
  }
  //网络请求
  wx.request({
    url: host + url,
    data: postData,
    method: method,
    header: header,
    success: res => { //服务器返回数据
      console.log('请求的接口为：' + url);
      console.log(res);
      //判断接口回调状态码是否在200-300之间
      if (res.statusCode > 199 && res.statusCode < 300) {
        success(res.data); //返回正确
      } else {
        if (error != null) {
          error(res.data); //返回错误
        }
      }
    },
    error: e => {
      if (error != null) {
        error(); //返回错误
      }
    }
  });
}

module.exports = {
  token: token,
  banners: banners,
  bannerLog: bannerLog,
  goods: goods,
  goodInfo: goodInfo,
  sales: sales,
  signDatas: signDatas,
  sign: sign,
  repairSign: repairSign,
  myCard: myCard,
  giveCheckPhone: giveCheckPhone,
  shareSetting: shareSetting,
  getUserPhone: getUserPhone,
  updateUserInfo: updateUserInfo,
  getUserStep: getUserStep,
  getStepRule: getStepRule,
  checkUserFirstLogin: checkUserFirstLogin,
  receiveBubble: receiveBubble,
  rankList: rankList,
  sendCard: sendCard,
  exchangeCard: exchangeCard,
  getShareBack: getShareBack,
  qrCode: qrCode,
  sendStep: sendStep,
  helpRecordData: helpRecordData,
  sendStepHistory: sendStepHistory,
  changeGoods: changeGoods,
  pointOrders: pointOrders,
  activeOrders: activeOrders,
  activityList: activityList,
  luck: luck,
  luckInfo: luckInfo,
  luckJoin: luckJoin,
  myLuck: myLuck,
  writeLuck: writeLuck,
};